package com.johanChan.controller;

import com.alibaba.fastjson.JSON;
import com.johanChan.utils.BuildTree;
import com.johanChan.utils.CommonUtils;
import com.johanChan.utils.Tree;
import com.johanChan.utils.TreeBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class TreeController {

    List<TreeBuilder.Node> nodeList ;
    List<Tree<TreeBuilder.Node>> treeList;
    {
        nodeList = CommonUtils.buildNode();
        treeList = CommonUtils.buildTree();
    }

    @ResponseBody
    @RequestMapping("/getTreeJson")
    public String getTreeJson() {
//        List<TreeBuilder.Node> nodeList = CommonUtils.buildNode();
//        String result = CommonUtils.buildTree(nodeList);
        List<Tree<TreeBuilder.Node>> resultList = BuildTree.build(treeList);
        String result = JSON.toJSONString(resultList);
        return result;
    }

    @ResponseBody
    @RequestMapping("/getChildren/{id}")
    public String getChildren(@PathVariable("id") Integer id) {
//        List<TreeBuilder.Node> nodeList = CommonUtils.buildNode();
        List<TreeBuilder.Node> result = CommonUtils.getChildren(id,nodeList);
//        String jsonStr = JSON.toJSONString(result);
        String jsonStr = CommonUtils.buildTree(result);
        return jsonStr;
    }

}
